package westh.ilib.struts.action.croDomainSearch;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.ilib.service.ResourceService;

public class SaveTaggingAction extends Action{
	private final static Log log = LogFactory.getLog(GetResourceAction.class);
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) 
	{
		
		//得到输入的Json-串
		log.debug("Save annotations.");
		DynaActionForm f = (DynaActionForm) form;
		String rscId = f.getString("resourceId");
		log.debug("resourceId="+rscId);
		//String rscCmt = f.getString("rscCmt");
		String rscCmt =(String)request.getParameter("rscCmt");
		log.debug("rscCmt="+rscCmt);
		//rscCmt=rscCmt.trim();
		//String rscScore = f.getString("rscScore");
		String rscScore =(String)request.getParameter("rscScore");
		//String rscScore =(String)request.getParameter("rscScore");
		//String rscTags = f.getString("rscTags");
		String rscTags =(String)request.getParameter("rscTags");
//		String rscReadStatus = f.getString("rscReadStatus");
		String rscReadStatus =(String)request.getParameter("rscReadStatus");
		
		String userId = request.getRemoteUser();
		if(userId==null)
			userId="1aa8d7a86c4c46cb8cf4bb33e0fb7c4a";
		
		ResourceService rscService = new ResourceService();
		
		String outString=rscService.saveResourceAnnoInfo(userId, rscId, rscCmt, rscScore, rscTags, rscReadStatus, log);
		
		request.setAttribute("outString", outString);
		System.out.println(outString);
		String retCode=""; //返回代码
		String retMsg="Faile in saving annotating information.";//返回结果信息
		String returnStr="fail";//跳转信息
		
		JSONObject jsonOut= JSONObject.fromObject(outString);
		retCode = jsonOut.getString("retCode");
		
		if (retCode.equals("5500")) 
		{
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			returnStr = "succ";

		}
		else if (retCode.equals("0131")) {
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			returnStr = "fail";
		}
		return mapping.findForward(returnStr);
	}
}
